package com.clouds.wms.dao;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.DatabaseException;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;

@Repository("cmsmkDao")
public class CmsmkDaoImpl implements CmsmkDao {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public long saveCmsmk(Cmsmk cmsmk) throws DatabaseException {
		String sql = "INSERT INTO cmsmk (mkab, mkac, mkad, mkae, mkaf) VALUES(:mkab, :mkac, :mkad, :mkae, :mkaf)";
		try {
			KeyHolder keyHolder = new GeneratedKeyHolder();
			SqlParameterSource param = new BeanPropertySqlParameterSource(cmsmk);
			namedParameterJdbcTemplate.update(sql,param, keyHolder);
			return keyHolder.getKey().longValue();
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}

	

	@Override
	public PagesBean getCmsmkList(String start, String end, String mkad, int offset, int pageSize) throws DatabaseException {
		PagesBean pb = new PagesBean();
		List<Cmsmk> list = new ArrayList<Cmsmk>();
		boolean hasWhere = false;
		StringBuffer sql = new StringBuffer("SELECT * FROM cmsmk ");
		if(start != null && !start.equals("") ) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append(" mkae>='" + start + " 00:00:00'");
		}
		if(end != null && !end.equals("")) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append(" mkae<='" + end + " 23:59:59'");
		}
		if(mkad != null && !mkad.equals("")) {
			hasWhere = Function.appendWhereIfNeed(sql, hasWhere);
			sql.append(" mkab='"+mkad+"'");
		}
		sql.append(" ORDER BY mkaa DESC");
		try {
			int totalCount = namedParameterJdbcTemplate.getJdbcOperations().queryForInt(Function.toCountSql(sql.toString()));
			if(totalCount!=0){
				list = namedParameterJdbcTemplate.getJdbcOperations().query(Function.mySqlQuery(sql.toString(),offset,pageSize,totalCount),new BeanPropertyRowMapper<Cmsmk>(Cmsmk.class));
			}
			pb.setCount(totalCount);
			pb.setData(list);
			return pb;
		} catch (EmptyResultDataAccessException e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}

}
